//index.js
//获取应用实例
const app = getApp()
var topOpacity = 0;
Page({
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "相册", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      showViewBox: true,
      bgColor: 'transparent',
      isShare: 0
    },
    baseUrl: app.baseUrl,
    videoUrl: app.videoUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,
    //requestUrl:"https://small.andwind.cc/ly_small_service/",
    navBarHeight: app.globalData.navBarHeight,
    shareTitle: '',
    shareImg: '',
    chooseList: [],
    chooseId: 0,
    // 列表数据
    list: [],
    // 数据列表加载中
    listDataLoading: false,
    // 瀑布流加载中
    waterfallLoading: false,
    // 数据加载完毕
    loaded: false,
    id: 1,
    page: 1,
    pagesize: 0,
    showList: [], //展示的数据
    rowCount: 0, //此分类下的总条数
    openid:'',

    albumIndex:0,
    albumvalue:'',
    albumlist:[]
  },
  onPageScroll(t) {
    if (t.scrollTop < 40) {
      topOpacity = 0
      if (this.data.topOpacity != 0) {
        this.setData({
          topOpacity: 0
        })
      }
    } else if (t.scrollTop >= 40) {
      if (topOpacity <= 1) {
        topOpacity = t.scrollTop / 100 - .4
      } else {
        topOpacity = 1
      }
      if (topOpacity <= 1) {
        this.setData({
          topOpacity: topOpacity
        })
      }
    }
  },
  
  bindMultiPickerChange(e){
    console.log(e);
    this.setData({
      albumvalue:this.data.albumlist[e.detail.value].name,
      chooseId:this.data.albumlist[e.detail.value].id,
      page:1
    })
    this.update();
  },
  

  //获取微信分享信息
  getShareInfo() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'getShareInfo.php',
      method: 'POST',
      data: {
       
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        that.setData({
          shareTitle: res.data.shareTitle,
          shareImg: res.data.shareImg
        });
      },
      fail: function (res) {}
    });
  },


  //页面初始加载
  onLoad(options) {
    var that = this;
    var id = options.id;
    if(id==0||id==''||id==null||id==undefined){
      app.showTip('抱歉,未获取到相册id哦');
      setTimeout(function(){
        wx.navigateBack({
          delta: 1
        });
      },1400);
      return;
    }else{
      that.setData({
        chooseId:id
      });
    }

    var is_share = options.is_share;
    //判断返回的跳转路径
    if (is_share == 1) {
      this.setData({
        ['defaultData.isShare']: 1
      });
    }

    //获取缓存中的openid
    var openid = wx.getStorageSync('ly24_openid');

    if (openid == '') {
      //缓存中没有openid
      //获取所需页面信息
      wx.login({
        success: function (res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: that.data.baseUrl + 'getOpenid.php',
              method: 'POST',
              data: {
                'code': res.code
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function (res) {
                //缓存微信openid
                wx.setStorageSync('ly24_openid', res.data.openid);
                that.setData({
                  openid: res.data.openid
                });

                //调用分享
                that.getBrochureIndexPageInfo(id);
              },
              fail: function (res) {}
            });
          }
        }
      });
    } else {
      that.setData({
        openid: openid
      });

     //获取本页数据
    that.getBrochureIndexPageInfo(id);
    }
    
    //调用分享
    that.getShareInfo();      
  },

  //获取本页数据
  getBrochureIndexPageInfo(id) {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'getBrochureIndexPageInfo.php',
      method: 'POST',
      data: {
        id:id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.status == 1) {
          that.setData({
            //chooseList: res.data.menuList,
            rowCount: parseInt(res.data.rowCount),
            pagesize: parseInt(res.data.pageSize),

            //albumIndex:0,
            albumvalue:res.data.albumlist[0].name,
            albumlist:res.data.albumlist,
            chooseId:res.data.albumlist[0].id,
            shareTitle:res.data.albumlist[0].name,
            shareImg:that.data.baseUrl+'admin/'+res.data.albumlist[0].img_path
          });

          that.update();
        }
      },
      fail: function (res) {}
    });
  },

  // 页面下拉刷新
  onPullDownRefresh() {
    this.setData({
      id: 1,
      page: 1
    });
    this.update()
  },

  // 页面触底
  onReachBottom() {
    this.loadMore()
  },

  // 加载更多
  loadMore() {
    var that = this;
    let {
      page,
      pagesize,
      list
    } = this.data;


    this.setData({
      page: page + 1,
      list
    })

    let totalPage = Math.floor(that.data.rowCount / pagesize);

    //后台请求分页
    if (page <= totalPage) {
      wx.request({
        url: that.data.baseUrl + 'getXcPageList.php',
        method: 'POST',
        data: {
          'mid': that.data.chooseId,
          'page': that.data.page
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          if (res.data.status == 1) {
            that.setData({
              showlist: res.data.list,
              rowCount: res.data.rowCount
            })

            let more = that.getMockData()
            list = [...list, ...more]
            that.setData({
              list
            });

            if (page >= totalPage) {
              that.setData({
                loaded: false
              });
            }
          }
        },
        fail: function (res) {}
      });
    } else {
      that.setData({
        loaded: false
      });
    }
  },

  // 刷新新瀑布流
  update() {
    var that = this;
    this.data.id = 1;

    // 重置瀑布流组件
    this.setData({
      loaded: false
    })

    this.selectComponent('#waterfall').reset();

    wx.request({
      url: that.data.baseUrl + 'getXcPageList.php',
      method: 'POST',
      data: {
        'mid': that.data.chooseId,
        'page': that.data.page
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.status == 1) {
          that.setData({
            showlist: res.data.list,
            rowCount: res.data.rowCount
          })

          let list = that.getMockData();

          that.setData({
            list
          })
          let totalPage = Math.floor(that.data.rowCount / that.data.pagesize);
          if (that.data.page >= totalPage) {
            that.setData({
              loaded: false
            });
          }
          wx.stopPullDownRefresh()
        }
      },
      fail: function (res) {}
    });


  },

  onLoadingChange(e) {
    this.setData({
      waterfallLoading: e.detail
    })
  },

  /**
   * 获取模拟数据
   */
  getMockData() {
    var that = this;
    let {
      id,
      listDataLoading,
      loaded,
      showlist
    } = this.data
    if (listDataLoading || loaded) return []
    this.setData({
      listDataLoading: true
    })
    let list = [];
    const imgWidth = 300
    for (let i = 0; i < showlist.length; i++) {

      list.push({
        id,
        imgUrl: showlist[i].imgUrl
      })

      this.data.id = ++id
    }
    this.setData({
      listDataLoading: false
    })
    if (id > that.data.rowCount) {
      this.setData({
        loaded: true
      })
    }
    return list
  },

  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.shareTitle,
      path: '/pages/activityCenter/album/index?id='+that.data.chooseId+'&is_share=1',
      imageUrl: that.data.shareImg,
      success: function (res) {

      },
      fail: function (res) {

      }
    }
  },

})